/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mosta
 */
public class OrderDataBase {
    
    private Connection conn;
    private Statement stmt;
    private ResultSet result;
    private PreparedStatement prepStmt;

    public OrderDataBase() {
        
        DatabaseConnection connection = new DatabaseConnection();
        conn = connection.startConnection();
    }
    public int fillOrder(OrderBean order)
    {
        int value=0;
        String chkSql="select c_id from order where c_id=? ";
        String sql="insert into order"
                +"(o_id,amount,c_id,confirmation_status)"
                +"VALUES (?,?,?,?)";
        try {
            prepStmt = conn.prepareStatement(chkSql);
            prepStmt.setInt(1,order.getoId());
            result = prepStmt.executeQuery();
            if(result.next())
            {
                return value;
            }
            else
            {
                prepStmt = conn.prepareStatement(sql);
                prepStmt.setInt(1, order.getoId());
                prepStmt.setInt(2, order.getAmount());
                prepStmt.setInt(3, order.getcId());
                prepStmt.setInt(4, order.getConf());
                value = prepStmt.executeUpdate();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return value;
    }
}
